/**
 * Learning Backbone -> Syncing Models with Views
 * 
 * @author: MPeg [marco(dot)pegoraro(at)gmail(dot)com]
 * 
 * This view listen for the form to be submitted and updates the model with form
 * data.
 * 
 */

define([ 'backbone', 'jquery','collections/directory_collection'

], function(Backbone, $,Directory

) {

	return Backbone.View.extend({

		el : $("#contacts"),

		initialize : function() {
			
			//demo data
		    var contacts = [
		        { name: "Contact 1", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
		        { name: "Contact 2", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
		        { name: "Contact 3", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
		        { name: "Contact 4", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
		        { name: "Contact 5", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
		        { name: "Contact 6", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
		        { name: "Contact 7", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
		        { name: "Contact 8", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" }
		    ];
			
			this.collection = new Directory(contacts);
			this.render();
		},

		render : function() {
			var that = this;
			_.each(this.collection.models, function(item) {
				that.renderContact(item);
			}, this);
		},

		renderContact : function(item) {
			var contactView = new ContactView({
				model : item
			});
			this.$el.append(contactView.render().el);
		}

	});

});